package com.zmovie.admin.controller.movie;

import com.zmovie.admin.constant.Constants;
import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.framework.domain.MovieRuntime;
import com.zmovie.framework.service.MovieRuntimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 电影播放时段模块
 * @since 2023-11-30 20:06
 */
@Controller
@RequestMapping("/movie/runtime")
@Api(tags = "电影播放时段模块")
public class MovieRuntimeController {
    @Autowired
    private MovieRuntimeService movieRuntimeService;

    @GetMapping
    @ApiOperation("电影播放时段首页")
    public String index(Model model) {
        List<MovieRuntime> movieRuntimes = movieRuntimeService.listMovieRuntimes(new MovieRuntime());
        model.addAttribute(Constants.MOVIE_RUNTIMES_KEY, movieRuntimes);
        return "movie/movie_runtime";
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取电影时段")
    @ResponseBody
    public Object getMovieRuntime(@PathVariable("id") Integer id) {
        return CommonReturnType.success(movieRuntimeService.getById(id));
    }

    @PostMapping
    @ApiOperation("添加电影时段")
    @ResponseBody
    public Object addMovieRuntime(@Validated MovieRuntime movieRuntime){
        movieRuntimeService.save(movieRuntime);
        return CommonReturnType.success("添加成功!");
    }

    @PutMapping
    @ApiOperation("修改电影时段")
    @ResponseBody
    public Object updateMovieRuntime(@Validated MovieRuntime movieRuntime) throws BusinessException {
        //参数校验
        if(movieRuntime == null || movieRuntime.getId() == null){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        movieRuntimeService.updateById(movieRuntime);
        return CommonReturnType.success("修改成功!");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除电影时段")
    @ResponseBody
    public Object deleteMovieRunTime(@PathVariable("id") @NotNull Integer id){
        movieRuntimeService.removeById(id);
        return CommonReturnType.success("删除成功!");
    }

    @DeleteMapping
    @ApiOperation("批量删除电影时段")
    @ResponseBody
    public Object deleteMovieRunTimeBatch(Integer[] id) throws BusinessException {
        if(id == null || id.length == 0){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        movieRuntimeService.removeByIds(Arrays.asList(id));
        return CommonReturnType.success("删除成功!");
    }

    @PostMapping("/search")
    @ApiOperation("搜索电影时段")
    public Object search(MovieRuntime movieRuntime, Model model){
        List<MovieRuntime> movieRuntimes = movieRuntimeService.listMovieRuntimes(movieRuntime);
        model.addAttribute(Constants.MOVIE_RUNTIMES_KEY, movieRuntimes);
        return "movie/movie_runtime :: runtimeList";
    }
}
